//
//  XPButton.m
//  QQ
//
//  Created by nhope on 2017/4/27.
//  Copyright © 2017年 xiaopin. All rights reserved.
//

#import "XPButton.h"

@implementation XPButton
{
    NSTrackingArea *_trackingArea;
}

#pragma mark - Lifecycle

- (instancetype)initWithFrame:(NSRect)frameRect {
    self = [super initWithFrame:frameRect];
    if (self) {
        [self setButtonType:NSButtonTypeMomentaryChange];
        [self setBezelStyle:NSBezelStyleRegularSquare];
        [self readdTrackingArea];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setButtonType:NSButtonTypeMomentaryChange];
    [self setBezelStyle:NSBezelStyleRegularSquare];
    [self readdTrackingArea];
}

- (void)viewWillMoveToWindow:(NSWindow *)newWindow {
    [super viewWillMoveToWindow:newWindow];
    // 从Storyboard中获取系统设置项
    self.normalImage = self.normalImage ?: self.image;
    self.pressImage = self.pressImage ?: self.alternateImage;
    self.normalTitle = self.normalTitle ?: self.title;
    self.pressTitle = self.pressTitle ?: self.alternateTitle;
    // 更新UI
    [self reloadUI];
}

#pragma mark - Mouse Event

- (void)updateTrackingAreas {
    [super updateTrackingAreas];
    [self readdTrackingArea];
}

/// 鼠标进入按钮,进入高亮状态
- (void)mouseEntered:(NSEvent *)event {
    if (![self isEnabled]) {
        return;
    }
    self.image = self.hoverImage ?: self.normalImage;
    NSString *title = self.hoverTitle ?: self.normalTitle;
    if (title.length) {
        NSAttributedString *attributedString = [self titleAttributedStringWithTitle:title titleColor:(self.hoverTitleColor ?: self.normalTitleColor)];
        [self setAttributedTitle:attributedString];
    }
}

/// 鼠标按钮,选中状态
- (void)mouseDown:(NSEvent *)event {
    if (![self isEnabled]) {
        return;
    }
    self.image = self.pressImage ?: self.normalImage;
    NSString *title = self.pressTitle ?: self.normalTitle;
    if (title.length) {
        NSAttributedString *attributedString = [self titleAttributedStringWithTitle:title titleColor:(self.pressTitleColor ?: self.normalTitleColor)];
        [self setAttributedTitle:attributedString];
    }
}

/// 鼠标抬起,恢复高亮状态
- (void)mouseUp:(NSEvent *)event {
    NSPoint clickPoint = [self convertPoint:[event locationInWindow] fromView:nil];
    if (![self isEnabled] || !NSPointInRect(clickPoint, self.bounds)) {
        return;
    }
    
    self.image = self.hoverImage ?: self.normalImage;
    NSString *title = self.hoverTitle ?: self.normalTitle;
    if (title.length) {
        NSAttributedString *attributedString = [self titleAttributedStringWithTitle:title titleColor:(self.hoverTitleColor ?: self.normalTitleColor)];
        [self setAttributedTitle:attributedString];
    }
    // 触发点击事件
    [NSApp sendAction:self.action to:self.target from:self];
}

/// 鼠标移出按钮,恢复普通状态/禁用状态
- (void)mouseExited:(NSEvent *)event {
    NSAttributedString *attributedString = nil;
    if ([self isEnabled]) {
        self.image = self.normalImage;
        attributedString = [self titleAttributedStringWithTitle:self.normalTitle titleColor:self.normalTitleColor];
    } else {
        NSColor *color = self.disableTitleColor ?: [NSColor grayColor];
        self.image = self.disableImage;
        attributedString = [self titleAttributedStringWithTitle:self.disableTitle titleColor:color];
    }
    [self setAttributedTitle:attributedString];
}

#pragma mark - Private

- (void)readdTrackingArea {
    if (nil != _trackingArea) {
        [self removeTrackingArea:_trackingArea];
    }
    if ([self isEnabled]) {
        _trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited|NSTrackingActiveAlways owner:self userInfo:nil];
        [self addTrackingArea:_trackingArea];
    }
}

- (void)reloadUI {
    self.image = (!self.enabled && self.disableImage) ? self.disableImage : self.normalImage;
    NSString *title = (!self.enabled && self.disableTitle.length) ? self.disableTitle : self.normalTitle;
    NSAttributedString *attributedString = [self titleAttributedStringWithTitle:title titleColor:(self.enabled ? self.normalTitleColor : self.disableTitleColor)];
    [self setAttributedTitle:attributedString];
}

- (NSAttributedString *)titleAttributedStringWithTitle:(NSString *)title titleColor:(NSColor *)titleColor {
    if (nil == title || 0 == title.length) {
        return nil;
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    NSDictionary *attributes = @{
                                 NSFontAttributeName: self.font,
                                 NSForegroundColorAttributeName: (titleColor?:[NSColor blackColor]),
                                 NSParagraphStyleAttributeName: paragraphStyle
                                 };
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:title attributes:attributes];
    return attributedString;
}

#pragma mark - setter & getter

- (void)setEnabled:(BOOL)enabled {
    [super setEnabled:enabled];
    [self readdTrackingArea];
    [self reloadUI];
}

@end
